﻿using System;
using System.Data.SqlClient;
using Company.BLL.WS.KDT.SXXK;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data;
using System.Text;
using System.Xml;
using System.Collections;

namespace Company.BLL.KDT.SXXK
{
    public partial class DinhMucDangKy
    {
        private DinhMucCollection _DMCollection = new DinhMucCollection();

        public DinhMucCollection DMCollection
        {
            set { this._DMCollection = value; }
            get { return this._DMCollection; }
        }

        //-----------------------------------------------------------------------------------------
        public void LoadDMCollection()
        {
            this._DMCollection = DinhMuc.SelectCollectionBy_Master_ID(this.ID);
        }

        
        #region IDangKy Members

        public bool InsertUpdateFull()
        {
            bool ret;
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection) db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
                    if (this._ID == 0)
                        this._ID = this.InsertTransaction(transaction);
                    else
                        this.UpdateTransaction(transaction);

                    foreach (DinhMuc dmDetail in this._DMCollection)
                    {
                        if (dmDetail.ID == 0)
                        {
                            dmDetail.Master_ID = this._ID;
                            dmDetail.ID = dmDetail.InsertTransaction(transaction);
                        }
                        else
                        {
                            dmDetail.UpdateTransaction(transaction);
                        }
                    }
                    transaction.Commit();
                    ret = true;
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw new Exception(ex.Message);
                }
                finally
                {
                    connection.Close();
                }
            }
            return ret;
        }

        public void InsertUpdateFull(SqlTransaction transaction)
        {
            if (this._ID == 0)
                this._ID = this.InsertTransaction(transaction);
            else
                this.UpdateTransaction(transaction);

            foreach (DinhMuc dmDetail in this._DMCollection)
            {
                if (dmDetail.ID == 0)
                {
                    dmDetail.Master_ID = this._ID;
                    dmDetail.ID = dmDetail.InsertTransaction(transaction);
                }
                else
                {
                    dmDetail.UpdateTransaction(transaction);
                }
            }
        }

        public long WSSend()
        {
            // Chuẩn bị dữ liệu để gửi qua WEB SERVICE.
            // Lấy thông tin đăng ký.
            KDT_SXXK_Service kdtService = new KDT_SXXK_Service();
            ThongTinDinhMucInFo webThongTinDM = new ThongTinDinhMucInFo();
            webThongTinDM.MA_HQTN = this.MaHaiQuan;
            webThongTinDM.MA_DV = this.MaDoanhNghiep;
            webThongTinDM.Ma_DVKB = this.MaDoanhNghiep;
            webThongTinDM.TrangThaiXL = 0;
            webThongTinDM.So_TNDKDT = this.SoTiepNhan;
            webThongTinDM.Nam_TN = (short)this.NgayTiepNhan.Year;
            webThongTinDM.Ngay_TN = this.NgayTiepNhan;
            // Lấy danh sách định mức.
            ArrayList list = new ArrayList();
            int i = 0;
            string masp = "";
            DinhMucDangKyInfo dmdk = null;
            ArrayList temp = new ArrayList();
            foreach (DinhMuc dmDetail in this._DMCollection)
            {
                if (masp != dmDetail.MaSanPham)
                {
                    masp = dmDetail.MaSanPham;
                    if (i != 0)
                    {
                        dmdk.DMInfoCollection = new DinhMucInfo[temp.Count];
                        for (int j = 0; j < temp.Count; ++j)
                        {
                            dmdk.DMInfoCollection[j] = (DinhMucInfo)temp[j];
                        }
                        temp = new ArrayList();
                        list.Add(dmdk);
                    }                  
                    dmdk = new DinhMucDangKyInfo();
                    dmdk.MaDoanhNghiep = this.MaDoanhNghiep;
                    dmdk.MaHaiQuan = this.MaHaiQuan;
                    dmdk.MaSP = dmDetail.MaSanPham;
                    dmdk.NgayDangKy = DateTime.Today;
                    dmdk.NgayApDung = DateTime.Today;
                    dmdk.TrangThaiXuLy = 0;                   
                    
                }
                DinhMucInfo webDMInfo = new DinhMucInfo();
                webDMInfo.MaHaiQuan = this._MaHaiQuan;
                webDMInfo.MaDoanhNghiep = this._MaDoanhNghiep;
                webDMInfo.MaSanPham = dmDetail.MaSanPham;
                webDMInfo.MaNguyenPhuLieu = dmDetail.MaNguyenPhuLieu;
                webDMInfo.SoThuTuHang = dmDetail.STTHang;
                webDMInfo.TyLeHaoHut = dmDetail.TyLeHaoHut;
                webDMInfo.DinhMucSuDung = dmDetail.DinhMucSuDung;
                temp.Add(webDMInfo);
                i++;
            }
            // dua thang cuoi cung vao
            dmdk.DMInfoCollection = new DinhMucInfo[temp.Count];
            for (int j = 0; j < temp.Count; ++j)
            {
                dmdk.DMInfoCollection[j] = (DinhMucInfo)temp[j];
            }            
            list.Add(dmdk);
            webThongTinDM.DinhMucSanPham = new DinhMucDangKyInfo[list.Count];
            for (int k = 0; k < list.Count; ++k)
            {
                webThongTinDM.DinhMucSanPham[k] = (DinhMucDangKyInfo)list[k];
            }            
            // Thực hiện gửi dữ liệu.
            this._SoTiepNhan = kdtService.DinhMuc_Send(webThongTinDM);
            if (this._SoTiepNhan > 0)
            {
                this._TrangThaiXuLy = BLL.TrangThaiXuLy.CHO_DUYET;
                this._NgayTiepNhan = DateTime.Today;
                this.NgayDangKy = DateTime.Today;
                this.NgayApDung = DateTime.Today;

                this.InsertUpdateFull();
            }
            return this._SoTiepNhan;
        }

        public void WSDownload()
        {
            // Các bước thực hiện:
            // 1. Lấy thông tin từ Web Service.
            // 2. Cập nhật thông tin đăng ký.
            // 3. Xóa danh sách sản phẩm cũ.
            // 4. Cập nhật lại danh sách sản phẩm mới.
            // 5. Nếu trạng thái: Đã duyệt chính thức thì chuyển danh sách sản phẩm đã duyệt sang phân hệ SXXK.

             //1. Lấy danh sách từ Web Service.
            KDT_SXXK_Service kdtService = new KDT_SXXK_Service();
            DataSet ds = kdtService.DinhMuc_RequestStatus(this._SoTiepNhan, this._MaHaiQuan, this._MaDoanhNghiep, Convert.ToInt16(this._NgayTiepNhan.Year));
            DataTable dt = ds.Tables[0];
            if (dt.Rows.Count > 0)
            {
                SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
                using (SqlConnection connection = (SqlConnection)db.CreateConnection())
                {
                    connection.Open();
                    SqlTransaction transaction = connection.BeginTransaction();
                    try
                    {
                        // 2. Cập nhật thông tin đăng ký.
                        this._TrangThaiXuLy = Convert.ToInt32(dt.Rows[0]["TrangThaiXL"]);

                        // 3. Xóa danh sách dinh muc cũ.
                        this.LoadDMCollection();
                        DinhMuc.DeleteCollection(this.DMCollection, transaction);
                        this.DMCollection.Clear();

                        // 4. Cập nhật lại danh sách dinh muc mới.
                        int stt = 1;
                        foreach (DataRow row in dt.Rows)
                        {
                             DinhMuc dm = new DinhMuc();
                            dm.Master_ID = this._ID;
                            dm.MaSanPham = row["Ma_SP"].ToString();
                            dm.MaNguyenPhuLieu = row["Ma_NPL"].ToString();
                            dm.TyLeHaoHut = Convert.ToDecimal(row["TL_HH"]);
                            dm.DinhMucSuDung = Convert.ToDecimal(row["DM_SD"]);                            
                            dm.GhiChu = row["GHI_CHU"].ToString();
                            dm.STTHang = stt++;
                            this.DMCollection.Add(dm);
                        }

                        // Cập nhật vào CSDL.
                        this.InsertUpdateFull(transaction);

                        // 5. Nếu trạng thái là đã duyệt chính thức thì chuyển danh sách sản phẩm đã duyệt sang phân hệ SXXK.
                        if (this._TrangThaiXuLy == BLL.TrangThaiXuLy.DA_DUYET)
                        {
                            foreach (DataRow row in ds.Tables[1].Rows)
                            {
                                //cap nhat thong tin dinh muc
                                BLL.SXXK.ThongTinDinhMuc ttdm = new Company.BLL.SXXK.ThongTinDinhMuc();
                                ttdm.MaDoanhNghiep = this.MaDoanhNghiep;
                                ttdm.MaHaiQuan = this.MaHaiQuan;
                                ttdm.MaSanPham = row["Ma_SP"].ToString();
                                ttdm.NgayApDung = Convert.ToDateTime(row["NGAY_AD"].ToString());
                                ttdm.NgayDangKy = Convert.ToDateTime(row["NGAY_DK"].ToString());
                                ttdm.SoDinhMuc = Convert.ToInt32(row["SO_DM"].ToString());
                                ttdm.InsertUpdateTransaction(transaction);
                            }
                            foreach (DataRow row in ds.Tables[0].Rows)
                            {                              
                                  //cap nhat dinh muc
                                BLL.SXXK.DinhMuc dm = new BLL.SXXK.DinhMuc();
                                dm.MaHaiQuan = this._MaHaiQuan;
                                dm.MaDoanhNghiep = this._MaDoanhNghiep;
                                dm.MaSanPHam = row["Ma_SP"].ToString();
                                dm.MaNguyenPhuLieu = row["Ma_NPL"].ToString();
                                dm.TyLeHaoHut = Convert.ToDecimal(row["TL_HH"]);
                                dm.DinhMucSuDung = Convert.ToDecimal(row["DM_SD"]);
                                dm.DinhMucChung = dm.DinhMucSuDung * (dm.TyLeHaoHut + 100) / 100;
                                dm.GhiChu = row["GHI_CHU"].ToString();
                                dm.InsertUpdateTransaction(transaction);
                            }
                        }

                        transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw new Exception(ex.Message);
                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            }
            else
            {
                this.Update();
            }
        }

        //-----------------------------------------------------------------------------------------
        /// <summary>
        /// Hủy thông tin khai báo.
        /// </summary>
        public bool WSCancel()
        {
            KDT_SXXK_Service kdtService = new KDT_SXXK_Service();
            bool result = kdtService.DinhMuc_Cancel(this._SoTiepNhan, this._MaHaiQuan, this._MaDoanhNghiep, this._NgayTiepNhan.Year);
            if (result)
            {
                // Trả lại trạng thái chưa khai báo.
                this.SoTiepNhan = 0;
                this.NgayTiepNhan = new DateTime(1900, 1, 1);
                this.NgayDangKy = new DateTime(1900, 1, 1);
                this.NgayApDung = new DateTime(1900, 1, 1);
                this.TrangThaiXuLy = BLL.TrangThaiXuLy.CHUA_KHAI_BAO;
                this.Update();
            }
            return result;
        }


        #endregion

        #region Webservice của Hải quan
        private string ConfigPhongBi(int type, int function)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("TemplateXML\\PhongBi.xml");
            //set thong tin hai quan den
            XmlNode nodeTo = doc.GetElementsByTagName("To")[0];
            XmlNode nodeName = nodeTo.ChildNodes[0];
            nodeName.InnerText = Company.BLL.DuLieuChuan.DonViHaiQuan.GetName(this._MaHaiQuan);
            XmlNode nodeIdentity = nodeTo.ChildNodes[1];
            nodeIdentity.InnerText = this.MaHaiQuan;

            ////set thong so gui di subject
            XmlNode nodeSubject = doc.GetElementsByTagName("Subject")[0];
            XmlNode nodeType = nodeSubject.ChildNodes[0];
            nodeType.InnerText = type.ToString();

            XmlNode nodeFunction = nodeSubject.ChildNodes[1];
            nodeFunction.InnerText = function.ToString();

            XmlNode nodeReference = nodeSubject.ChildNodes[2];
            nodeReference.InnerText = (System.Guid.NewGuid().ToString());

            XmlNode nodeMessageID = doc.GetElementsByTagName("messageId")[0];
            nodeMessageID.InnerText = (System.Guid.NewGuid().ToString());

            return doc.InnerXml;
        }

        #region Khai báo danh sách sản phẩm
        public string WSSendXML(string pass)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(ConfigPhongBi(MessgaseType.DinhMuc, MessgaseFunction.KhaiBao));
            XmlDocument docNPL = new XmlDocument();
            docNPL.LoadXml(ConvertCollectionToXML());

            //luu vao string
            XmlNode root = docNPL.GetElementsByTagName("Root")[0].CloneNode(true);
            XmlNode Content = doc.GetElementsByTagName("Content")[0];
            XmlNode root2 = doc.CreateElement("Root");
            root2.InnerXml = root.InnerXml;
            Content.AppendChild(root2);
            WS.KhaiDienTu.KDTService kdt = new Company.BLL.WS.KhaiDienTu.KDTService();
            string kq = kdt.Send(doc.InnerXml, pass);
            return kq;
        }
        private string ConvertCollectionToXML()
        {
            //load du lieu
            XmlDocument docDM = new XmlDocument();
            docDM.Load("TemplateXML\\KhaiBaoDinhMuc.xml");
            XmlNode DinhMucNode = docDM.GetElementsByTagName("DINH_MUC")[0];
            XmlNode nodeHQNhan = docDM.GetElementsByTagName("HQ_NHAN")[0];
            nodeHQNhan.Attributes["MA_HQ"].Value = this.MaHaiQuan;
            nodeHQNhan.Attributes["TEN_HQ"].Value = Company.BLL.DuLieuChuan.DonViHaiQuan.GetName(this._MaHaiQuan);

            if (this.DMCollection == null || this.DMCollection.Count == 0)
            {
                this.DMCollection = DinhMuc.SelectCollectionBy_Master_ID(this.ID);
            }
            string st="";
            XmlNode nodeSP = null;
            foreach (DinhMuc dm in this.DMCollection)
            {
                if(dm.MaSanPham!=st)
                {
                    st = dm.MaSanPham;
                    nodeSP = docDM.CreateElement("DINH_MUC.SP");
                    XmlAttribute attMASP =docDM.CreateAttribute("MA_SP");
                    attMASP.Value=dm.MaSanPham;
                    nodeSP.Attributes.Append(attMASP);
                    DinhMucNode.AppendChild(nodeSP);
                }
                XmlNode nodeNPL = docDM.CreateElement("DINH_MUC.SP.ITEM");

                XmlAttribute sttAtt = docDM.CreateAttribute("STT");
                sttAtt.Value = dm.STTHang.ToString();
                nodeNPL.Attributes.Append(sttAtt);

                XmlAttribute maAtt = docDM.CreateAttribute("MA_NPL");
                maAtt.Value = dm.MaNguyenPhuLieu;
                nodeNPL.Attributes.Append(maAtt);

                XmlAttribute DinhMucSDAtt = docDM.CreateAttribute("DINH_MUC_SD");
                DinhMucSDAtt.Value = dm.DinhMucSuDung.ToString();
                nodeNPL.Attributes.Append(DinhMucSDAtt);

                XmlAttribute TyLeHHAtt = docDM.CreateAttribute("TY_LE_HH");
                TyLeHHAtt.Value = dm.TyLeHaoHut.ToString();
                nodeNPL.Attributes.Append(TyLeHHAtt);
                nodeSP.AppendChild(nodeNPL);
            }            
            return docDM.InnerXml;
        }
        #endregion Khai báo danh sách nguyên phụ liệu

        #region Huy khai báo danh mục nguyên phụ liệu

        public string WSCancelXML(string pass)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(ConfigPhongBi(MessgaseType.DinhMuc, MessgaseFunction.HuyKhaiBao));

            XmlDocument docNPL = new XmlDocument();
            docNPL.Load("TemplateXML\\HuyKhaiBaoDinhMuc.xml");

            XmlNode nodeHQNhan = docNPL.GetElementsByTagName("HQ_NHAN")[0];
            nodeHQNhan.Attributes["MA_HQ"].Value = this.MaHaiQuan;
            nodeHQNhan.Attributes["TEN_HQ"].Value = Company.BLL.DuLieuChuan.DonViHaiQuan.GetName(this._MaHaiQuan);

            XmlNode nodeDuLieu = docNPL.GetElementsByTagName("DU_LIEU")[0];
            nodeDuLieu.Attributes["SO_TN"].Value = this.SoTiepNhan.ToString();
            nodeDuLieu.Attributes["NAM_TN"].Value = this.NgayTiepNhan.Year.ToString();

            XmlNode root = docNPL.GetElementsByTagName("Root")[0].CloneNode(true);
            XmlNode Content = doc.GetElementsByTagName("Content")[0];
            XmlNode root2 = doc.CreateElement("Root");

            root2.InnerXml = root.InnerXml;
            Content.AppendChild(root2);
            WS.KhaiDienTu.KDTService kdt = new Company.BLL.WS.KhaiDienTu.KDTService();
            string kq = kdt.Send(doc.InnerXml, pass);
            return kq;

        }

        #endregion Huy khai báo danh mục nguyên phụ liệu

        #region Lấy phản hồi

        public string WSRequestXML(string pass)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(ConfigPhongBi(MessgaseType.DanhMucNguyenPhuLieu, MessgaseFunction.LayPhanHoi));

            XmlDocument docNPL = new XmlDocument();
            docNPL.Load("TemplateXML\\LayPhanHoiDinhMuc.xml");

            XmlNode nodeHQNhan = docNPL.GetElementsByTagName("HQ_NHAN")[0];
            nodeHQNhan.Attributes["MA_HQ"].Value = this.MaHaiQuan;
            nodeHQNhan.Attributes["TEN_HQ"].Value = Company.BLL.DuLieuChuan.DonViHaiQuan.GetName(this._MaHaiQuan);

            XmlNode nodeDuLieu = docNPL.GetElementsByTagName("DU_LIEU")[0];
            nodeDuLieu.Attributes["SO_TN"].Value = this.SoTiepNhan.ToString();
            nodeDuLieu.Attributes["NAM_TN"].Value = this.NgayTiepNhan.Year.ToString();

            XmlNode root = docNPL.GetElementsByTagName("Root")[0].CloneNode(true);
            XmlNode Content = doc.GetElementsByTagName("Content")[0];
            XmlNode root2 = doc.CreateElement("Root");

            root2.InnerXml = root.InnerXml;
            Content.AppendChild(root2);

            WS.KhaiDienTu.KDTService kdt = new Company.BLL.WS.KhaiDienTu.KDTService();
            string kq = kdt.Request(doc.InnerXml, pass);
            return kq;

        }

        #endregion Lấy phản hồi
        #endregion Webservice của Hải quan

        #region Webservice của FPT

        #region Khai báo danh sách nguyên phụ liệu
        //cap nhat
        public string WSUpdateXMLFPT(string pass, DinhMucCollection collection)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("FPTService\\SXXK_DM_CAPNHAT.xml");

            //luu thong tin gui
            XmlNode hq = doc.SelectSingleNode("Root/SXXK/THONG_TIN/HQ_NHAN");
            hq.Attributes["MA_HQ"].Value = this.MaHaiQuan;
            hq.Attributes["TEN_HQ"].Value = Company.BLL.DuLieuChuan.DonViHaiQuan.GetName(this.MaHaiQuan);

            XmlNode data = doc.SelectSingleNode("Root/SXXK/THONG_TIN/DU_LIEU");
            data.Attributes["SO_DK"].Value = this.SoTiepNhan.ToString();

            XmlNode dmNode = doc.SelectSingleNode("Root/SXXK/THONG_TIN/DU_LIEU/DINH_MUC");
            XmlNode nodeSP = null;
            string st = "";
            foreach (DinhMuc dm in collection)
            {
                if (dm.MaSanPham != st)
                {
                    st = dm.MaSanPham;
                    nodeSP = doc.CreateElement("DINH_MUC.SP");
                    XmlAttribute attMASP = doc.CreateAttribute("MA_SP");
                    attMASP.Value = dm.MaSanPham;
                    nodeSP.Attributes.Append(attMASP);
                    dmNode.AppendChild(nodeSP);
                }
                XmlNode nodeNPL = doc.CreateElement("DINH_MUC.SP.ITEM");

                XmlAttribute maAtt = doc.CreateAttribute("MA_NPL");
                maAtt.Value = dm.MaNguyenPhuLieu;
                nodeNPL.Attributes.Append(maAtt);

                XmlAttribute DinhMucSDAtt = doc.CreateAttribute("DINH_MUC_SD");
                DinhMucSDAtt.Value = dm.DinhMucSuDung.ToString();
                nodeNPL.Attributes.Append(DinhMucSDAtt);

                XmlAttribute TyLeHHAtt = doc.CreateAttribute("TY_LE_HH");
                TyLeHHAtt.Value = dm.TyLeHaoHut.ToString();
                nodeNPL.Attributes.Append(TyLeHHAtt);
                nodeSP.AppendChild(nodeNPL);
            }    
            WS.FPT.SXXK.SXXKService service = new Company.BLL.WS.FPT.SXXK.SXXKService();
            string kq = service.DataProcess(doc.InnerXml, "ecusxgeiyklgA\\SmqUK@rbiqDyAgbXBIlBTUTL;", pass);
            return kq;
        }

        //dang ky
        public string WSSendXMLFPT(string pass)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("FPTService\\SXXK_DM_DK.xml.xml");

            //luu thong tin gui
            XmlNode hq = doc.SelectSingleNode("Root/SXXK/THONG_TIN/HQ_NHAN");
            hq.Attributes["MA_HQ"].Value = this.MaHaiQuan;
            hq.Attributes["TEN_HQ"].Value = Company.BLL.DuLieuChuan.DonViHaiQuan.GetName(this.MaHaiQuan);


            XmlNode dmNode = doc.SelectSingleNode("Root/SXXK/THONG_TIN/DU_LIEU/DINH_MUC");
            XmlNode nodeSP = null;
            string st = "";

            foreach (DinhMuc dm in this._DMCollection)
            {
                if (dm.MaSanPham != st)
                {
                    st = dm.MaSanPham;
                    nodeSP = doc.CreateElement("DINH_MUC.SP");
                    XmlAttribute attMASP = doc.CreateAttribute("MA_SP");
                    attMASP.Value = dm.MaSanPham;
                    nodeSP.Attributes.Append(attMASP);
                    dmNode.AppendChild(nodeSP);
                }
                XmlNode nodeNPL = doc.CreateElement("DINH_MUC.SP.ITEM");

                XmlAttribute maAtt = doc.CreateAttribute("MA_NPL");
                maAtt.Value = dm.MaNguyenPhuLieu;
                nodeNPL.Attributes.Append(maAtt);

                XmlAttribute DinhMucSDAtt = doc.CreateAttribute("DINH_MUC_SD");
                DinhMucSDAtt.Value = dm.DinhMucSuDung.ToString();
                nodeNPL.Attributes.Append(DinhMucSDAtt);

                XmlAttribute TyLeHHAtt = doc.CreateAttribute("TY_LE_HH");
                TyLeHHAtt.Value = dm.TyLeHaoHut.ToString();
                nodeNPL.Attributes.Append(TyLeHHAtt);
                nodeSP.AppendChild(nodeNPL);
            }    
            WS.FPT.SXXK.SXXKService service = new Company.BLL.WS.FPT.SXXK.SXXKService();
            string kq = service.DataProcess(doc.InnerXml, "ecusxgeiyklgA\\SmqUK@rbiqDyAgbXBIlBTUTL;", pass);
            return kq;

        }

        #endregion Khai báo danh sách nguyên phụ liệu

        #region Hủy khai báo danh sách nguyên phụ liệu

        //huy dang ky
        public string WSHuyXMLFPT(string pass)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("FPTService\\SXXK_DM_HUY.xml");

            //luu thong tin gui
            XmlNode hq = doc.SelectSingleNode("Root/SXXK/THONG_TIN/HQ_NHAN");
            hq.Attributes["MA_HQ"].Value = this.MaHaiQuan;
            hq.Attributes["TEN_HQ"].Value = Company.BLL.DuLieuChuan.DonViHaiQuan.GetName(this.MaHaiQuan);

            XmlNode nplNode = doc.SelectSingleNode("Root/SXXK/THONG_TIN/DU_LIEU");
            nplNode.Attributes["SO_DK"].Value = this.SoTiepNhan.ToString();
            WS.FPT.SXXK.SXXKService service = new Company.BLL.WS.FPT.SXXK.SXXKService();
            string kq = service.DataProcess(doc.InnerXml, "ecusxgeiyklgA\\SmqUK@rbiqDyAgbXBIlBTUTL;", pass);
            return kq;
        }

        #endregion Hủy khai báo danh sách nguyên phụ liệu

        #region Lấy thông tin trả lời từ hải quan

        //lây thong tin dang ky
        public string WSRequestXMLFPT(string pass)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("FPTService\\SXXK_TRALOI_DK.xml");

            //luu thong tin gui
            XmlNode dangkyType = doc.SelectSingleNode("Root/SXXK");
            dangkyType.Attributes["TRA_LOI"].Value = "DANG_KY_DM";
            XmlNode hq = doc.SelectSingleNode("Root/SXXK/THONG_TIN/HQ_NHAN");
            hq.Attributes["MA_HQ"].Value = this.MaHaiQuan;
            hq.Attributes["TEN_HQ"].Value = Company.BLL.DuLieuChuan.DonViHaiQuan.GetName(this.MaHaiQuan);

            XmlNode data = doc.SelectSingleNode("Root/SXXK/DU_LIEU");
            data.Attributes["SO_DK"].Value = this.SoTiepNhan.ToString();
            WS.FPT.SXXK.SXXKService service = new Company.BLL.WS.FPT.SXXK.SXXKService();
            string kq = service.DataProcess(doc.InnerXml, "ecusxgeiyklgA\\SmqUK@rbiqDyAgbXBIlBTUTL;", pass);
            return kq;
        }

        #endregion Lấy thông tin trả lời từ hải quan
        #endregion Webservice của FPT


    }
}